﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        
        int rightQuestion=0;
        const int MAX_BUTTONS = 6;
        private Operations currentOperations;
        Subject currentSubject;
        Person currentPerson;
        Random rd = new Random();
        RadioButton[] radioButtons = new RadioButton[MAX_BUTTONS];
        Question currentQuestion;
        public MainForm(Operations o, Person p, Subject s)
        {

            InitializeComponent();
            currentOperations = o;
            currentPerson = p;
            currentSubject = s;
            for (int i = 0; i < MAX_BUTTONS; i++)
            {
                radioButtons[i] = new RadioButton();
                radioButtons[i].Location = new Point(20, 170 + i * 30);
                radioButtons[i].Text = i.ToString();
                radioButtons[i].CheckedChanged += new EventHandler(MainForm_CheckedChanged);
                radioButtons[i].AutoSize = true;
                this.Controls.Add(radioButtons[i]);
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            int Num = int.Parse(mtb_Num.Text);
            currentOperations.GenerateTest(Num);

            currentQuestion = currentOperations.GetQuestion();
            rtb_Quest.Enabled = true;
            ShowQuestion();
            btn_Continue.Enabled = false;
            btn_Start.Visible = false;
        }

        void MainForm_CheckedChanged(object sender, EventArgs e)
        {
            btn_Continue.Enabled = true;
        }
        private void ShowQuestion()
        {
            Question q = currentQuestion;
            rtb_Quest.Text = q.FullQuest;
            for (int i = 0; i < q.Var.Length; i++)
            {
                radioButtons[i].Text = q.Var[i];
                radioButtons[i].Visible = true;
            }

            for (int i = q.Var.Length; i < MAX_BUTTONS; i++)
                radioButtons[i].Visible = false;
            this.AutoSize = true;
        }

        private void btn_Continue_Click(object sender, EventArgs e)
        {           
            foreach (RadioButton rb in radioButtons)
            {
                if (rb.Checked==true && rb.Text == currentQuestion.RightVar)
                {
                    rightQuestion++;
                }
                rb.Checked = false;
            }
            currentQuestion = currentOperations.GetQuestion();
            if (currentQuestion != null)
            {
                ShowQuestion();
                btn_Continue.Enabled = false;
            }
            else  
            {
                for (int i = 0; i < MAX_BUTTONS; i++)
                {
                    radioButtons[i].Visible = false;
                }
                rtb_Quest.Text = "Фамилия: " + currentPerson.LastName +"\nИмя: " + currentPerson.FirstName+ "\nПо дисциплине " + "\""+currentSubject.Title+"\"" +
                " из выбранных " + int.Parse(mtb_Num.Text) + " вопросов Вы ответили верно на " + rightQuestion;
                btn_Subject.Visible = true;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < MAX_BUTTONS; i++)
            {
                radioButtons[i].Visible = false;
            }
            rtb_Quest.Enabled = false;
            btn_Continue.Enabled = false;
            btn_Subject.Visible = false;
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите выйти?", "Выход", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                Application.Exit();
        }

        private void btn_Subject_Click(object sender, EventArgs e)
        {
            this.Close();
            StartForm sf = new StartForm();
            sf.Show();
        }
    }
}
